/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-05-23
 * Time: 12:22
 */
class Solution8 {
    public int change(int amount, int[] coins) {
        int n = coins.length;

        int[][] dp = new int[n+1][amount+1];

        dp[0][0] = 1;
        //列不用初始化 行初始化为0 0种选法无需特殊处理

        for(int i = 1; i <= n; i++) {
            for(int j = 0; j <= amount; j++) {
                dp[i][j] = dp[i-1][j];
                if(j >= coins[i-1]) {
                    dp[i][j] += dp[i][j-coins[i-1]];
                }
            }
        }
        return dp[n][amount];
    }
}

//优化后
//class Solution8 {
//    public int change(int amount, int[] coins) {
//        int n = coins.length;
//
//        int[] dp = new int[amount+1];
//
//        dp[0] = 1;
//        //列不用初始化 行初始化为0 0种选法无需特殊处理
//
//        for(int i = 1; i <= n; i++) {
//            for(int j = coins[i-1]; j <= amount; j++) {
//                dp[j] += dp[j-coins[i-1]];
//            }
//        }
//        return dp[amount];
//    }
//}